package com.lcw.library.imagepicker.loader;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import com.lcw.library.imagepicker.data.MediaFile;

/**
 * 视频扫描器
 * 
 * 扫描设备中的视频文件，支持：
 * - 按文件夹分组
 * - 获取视频时长
 * - 按时间排序
 * - 支持各种视频格式
 * 
 */
public class VideoScanner extends AbsMediaScanner<MediaFile> {

    private Context mContext;

    public VideoScanner(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected Uri getScanUri() {
        return MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    }

    @Override
    protected String[] getProjection() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10+ 分区存储，添加更多字段
            return new String[]{
                    MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.BUCKET_ID,
                    MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Video.Media.DURATION,
                    MediaStore.Video.Media.DATE_MODIFIED,
                    MediaStore.Video.Media.SIZE
            };
        } else {
            // Android 9及以下，保持原有字段
            return new String[]{
                    MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.BUCKET_ID,
                    MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Video.Media.DURATION,
                    MediaStore.Video.Media.DATE_MODIFIED
            };
        }
    }

    @Override
    protected String getSelection() {
        return null;
    }

    @Override
    protected String[] getSelectionArgs() {
        return null;
    }

    @Override
    protected String getOrder() {
        return MediaStore.Video.Media.DATE_MODIFIED + " desc";
    }

    /**
     * 构建媒体对象
     *
     * @param cursor
     * @return
     */
    @Override
    protected MediaFile parse(Cursor cursor) {
        // 获取媒体ID
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
        
        // 获取文件路径（在分区存储环境下可能为空或不可访问）
        String path = null;
        int dataColumnIndex = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
        if (dataColumnIndex != -1) {
            path = cursor.getString(dataColumnIndex);
        }
        
        // 构建Uri（推荐在Android 10+使用）
        Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
        
        String mime = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
        Integer folderId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_ID));
        String folderName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME));
        long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
        long modified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED));
        
        // 获取文件大小（Android 10+）
        long size = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            int sizeColumnIndex = cursor.getColumnIndex(MediaStore.Video.Media.SIZE);
            if (sizeColumnIndex != -1) {
                size = cursor.getLong(sizeColumnIndex);
            }
        }

        MediaFile mediaFile = new MediaFile();
        mediaFile.setId(id);
        mediaFile.setPath(path);
        mediaFile.setUri(uri);
        mediaFile.setMime(mime);
        mediaFile.setFolderId(folderId);
        mediaFile.setFolderName(folderName);
        mediaFile.setDuration(duration);
        mediaFile.setDateModified(modified);
        mediaFile.setSize(size);

        return mediaFile;
    }
}
